Fit trend models

Author

Max Lindmark

Published

September 5, 2023

Intro

This scripts fits models to gridded predictions of biomass trends as a function of climate trends (from climate-agnostic sdm, “04-fit-sdms-random.qmd”)

Load packages & source functions

# Load libraries, install if needed
pkgs <- c("tidyverse", "readxl", "tidylog", "RCurl", "devtools", "rMR", "ggridges",
          "kableExtra", "viridis", "RColorBrewer", "here", "sdmTMBextra") 

if(length(setdiff(pkgs,rownames(installed.packages()))) > 0){

    install.packages(setdiff(pkgs, rownames(installed.packages())), dependencies = T)
  
  }

invisible(lapply(pkgs, library, character.only = T))

# Packages not on CRAN or dev version
# remotes::install_github("pbs-assess/sdmTMB", dependencies = TRUE)
library(sdmTMB)

# Source code for map plots
# You need: # devtools::install_github("seananderson/ggsidekick") # not on CRAN; library(ggsidekick)
devtools::source_url("https://raw.githubusercontent.com/maxlindmark/pred-prey-overlap/main/R/functions/map-plot.R")
options(ggplot2.continuous.colour = "viridis")

# Set path
home <- here::here()

Read and plot data

# Read from GH?
d <- read_csv(paste0(home, "/data/clean/trends.csv")) |> 
  #mutate(quarter_f = as.factor(quarter)) |> 
  filter(quarter == 4) # probably there are ways to model this but for now we focus on Q4 because it's warmer and with less oxygen
Rows: 167822 Columns: 31
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (4): id, species, life_stage, group
dbl (27): X, Y, quarter, temp_slope, biom_slope, oxy_slope, phi_slope, upr_t...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
filter: removed 87,036 rows (52%), 80,786 rows remaining

What is the effect of temperature given oxygen? first check correlation

d |> group_by(group) |> summarise(cor = cor(temp_slope_sc, oxy_slope_sc))
group_by: one grouping variable (group)
summarise: now 6 rows and 2 columns, ungrouped
# A tibble: 6 × 2
  group               cor
  <chr>             <dbl>
1 Cod_Adult         0.240
2 Cod_Juvenile      0.182
3 Flounder_Adult    0.399
4 Flounder_Juvenile 0.185
5 Plaice_Adult      0.177
6 Plaice_Juvenile   0.165